package com.demo.modules.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.common.Res;
import com.demo.common.utils.UserUtils;
import com.demo.modules.entity.Detection;
import com.demo.modules.service.DetectionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (detection)核酸检测查询表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("detection")
public class DetectionController {
    /**
     * 服务对象
     */
    @Resource
    private DetectionService detectionService;

    /**
     * 查询核酸检测结果数据
     *
     * @return 分页数据
     */
    @GetMapping("list")
    public Res<List<Detection>> selectList() {
        List<Detection> list = detectionService.selectPage(UserUtils.getUserId());
        return Res.success(list);
    }

    /**
     * 根据id值在表里是否存在判断是新增还是修改操作
     * @param entity
     * @return
     */
    @PostMapping("saveOrUpdate")
    public Res<String> saveOrUpdate(@RequestBody Detection entity){
        if(entity.getUserId() == null){
            entity.setUserId(UserUtils.getUserId());
        }
        boolean result = detectionService.saveOrUpdate(entity);
        if(result){
            return Res.successMsg("新增或修改成功");
        }
        return Res.errorMsg("新增或修改失败");
    }

}
